using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
using System.Configuration;

namespace DAL
{
  public class tbNhanVienDAL
    {
           private SqlConnection cn = null;
        public tbNhanVienDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbNhanVienDAL(tbNhanVienDAL tan)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public void Insert(tbNhanVien th)
        {
            SqlCommand cm = new SqlCommand("tbNhanVien_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@taikhoan", th.taikhoan);
            cm.Parameters.AddWithValue("@matkhau", th.matkhau);
            cm.Parameters.AddWithValue("@hotennv", th.hotennv);
            cm.Parameters.AddWithValue("@diachi", th.diachi);
            cm.Parameters.AddWithValue("@dienthoai", th.dienthoai);
            cm.Parameters.AddWithValue("@email", th.email); 

            cm.ExecuteNonQuery();
        }
        public void Update(tbNhanVien th)
        {
            SqlCommand cm = new SqlCommand("tbNhanVien_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@manv", th.manv);
            cm.Parameters.AddWithValue("@taikhoan", th.taikhoan);
            cm.Parameters.AddWithValue("@matkhau", th.matkhau);
            cm.Parameters.AddWithValue("@hotennv", th.hotennv);
            cm.Parameters.AddWithValue("@diachi", th.diachi);
            cm.Parameters.AddWithValue("@dienthoai", th.dienthoai);
            cm.Parameters.AddWithValue("@email", th.email); 
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tbNhanVien_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@manv", iD);
            cm.ExecuteNonQuery();
        }
        public tbNhanVienCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tbNhanVien_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbNhanVienCollection nhanvienCollection = new tbNhanVienCollection();
            while (dr.Read())
            {
                tbNhanVien th = new tbNhanVien();
                th.manv = int.Parse(dr.GetValue(0).ToString());
                th.taikhoan = dr.GetValue(1).ToString();
                th.matkhau = dr.GetValue(2).ToString();
                th.hotennv = dr.GetValue(3).ToString();
                th.diachi = dr.GetValue(4).ToString();
                th.dienthoai = dr.GetValue(5).ToString();
                th.email = dr.GetValue(6).ToString();
                nhanvienCollection.Add(th);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return nhanvienCollection;
        }
    }
}
